import index from '../views/index.vue'
import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'index',
    component: index,
    meta: {
      title: '表单设计器',
    }
  },
  {
    path: '/example',
    name: 'example',
    component: import('../views/example/index.vue'),
    meta: {
      title: '表单渲染器',
    },
    redirect: '/example/base01',
    children: [
      {
        path: 'base01',
        component: import('../views/example/base01/index.vue'),
      },
      {
        path: 'base02',
        component: import('../views/example/base02/index.vue'),
      },
      {
        path: 'base03',
        component: import('../views/example/base03/index.vue'),
      },
      {
        path: 'base04',
        component: import('../views/example/base04/index.vue'),
      },
      {
        path: 'base05',
        component: import('../views/example/base05/index.vue'),
      },
    ]
  },
  {
    path: '/codemirror',
    name: 'codemirror',
    component: import('../views/codemirror/index.vue'),
    meta: {
      title: '代码编辑器',
    }
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  if (typeof to.meta?.title === 'string') {
    document.title = to.meta?.title
  } else {
    document.title = '表单设计器'
  }
  next()
})

export default router
